package com.monitorRoutine.monitorRoutineController;

import com.monitorRoutine.monitorRoutineController.util.YamlReaderUtil;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用于初始化数据、配置
 */
@Component
@Log
public class Initialize {
    @Value("${format.configuration-files}")
    String configurationFiles;
    public static Map<String, String> configurationMapper;

    @PostConstruct
    void initialize() throws Exception {
        log.info("开始初始化");
        log.info("初始化——校验文件");
        createFile(configurationFiles);
        log.info("初始化——校验文件数据");

        String[] params = new String[]{"monitorRoutineController.file.savePath",
                "monitorRoutineController.file.decompressionPath",
                "monitorRoutineController.format.serverAddress",
                "monitorRoutineController.format.name"};
        configurationMapper = YamlReaderUtil.readConfig(configurationFiles, params);
        log.info("初始化结束");
    }

    private void createFile(String filePath) {
        Path directory = Paths.get(filePath);
        try {
            if (!Files.exists(directory)) {
                Files.createDirectories(directory);
                log.info(filePath + ": 创建成功");
            } else {
                log.info(filePath + ": 存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.severe(filePath + ": 创建失败");
        }
    }
}
